//
//  SCNAnnouncementViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/4/25.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNAnnouncementViewController: SCNTableViewController {

    var data = [SCNAnnouncementListModels]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "站内公告"
        
        self.createTableView(CGRect(x: 0, y: SCNConstants.NavigationHeight, width: SCNConstants.SCREENWIDTH, height: SCNConstants.SCREENHEIGHT - SCNConstants.NavigationHeight), style: .plain, isRefreshNewData: true, isRefreshMoreData: true) { (table) in
            
            table.rowHeight = 60
            
            table.register(SCNAnnouncementTableViewCell.classForCoder(), forCellReuseIdentifier: "announcement")
        }
        
        request()
    }

    

    override func request() {
        
        super.request()

        let params = ["p":"\(self.pageIndex)","num":"20","cateid":"1"]

        SCNAnnouncementViewModels().requestOtherNoticeNew(params, successBlock: { [unowned self] (result) in
            
            if self.pageIndex == 1 {
                self.data.removeAll()
            }
            
            if let list = result.list {
                for tmp in list {
                    self.data.append(tmp)
                }
                self.tableView.reloadData()
                
            }
            self.dataTotal(result.total!)
            
        })
    }

}
extension SCNAnnouncementViewController {
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return data.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "announcement", for: indexPath) as! SCNAnnouncementTableViewCell
        
        cell.selectionStyle = .none
        
        cell.titleL.text = data[indexPath.row].title
        cell.timeL.text = data[indexPath.row].timeadd
        
        return cell
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        defer {
            tableView.deselectRow(at: indexPath, animated: true)
        }
        if let tmp = data[indexPath.row].url {
            self.loadWeb(at: self, url: tmp, title: "")
        }

    }
    
    
}
